/*
 * HandleRefCountMap.h
 *
 *  Created on: 2023年2月21日
 *      Author: terry
 */

#ifndef HANDLEREFCOUNTMAP_H_
#define HANDLEREFCOUNTMAP_H_

#include "BasicType.h"
#include <string>
#include <set>
#include <vector>
#include <mutex>
#include <map>
#include <functional>


class HandleRefCountMap
{
public:
	typedef void*	Handle;
	typedef std::vector< void* >	HandleArray;
	typedef std::vector< std::string >	StringArray;

	typedef std::pair< std::string, size_t >	KeyRefCount;
	typedef std::vector< KeyRefCount >			KeyRefCountArray;


public:
	HandleRefCountMap();
	virtual ~HandleRefCountMap();

	bool emptyHandle(const std::string& k);

	size_t countHandle(const std::string& k);

	bool existsHandle(const std::string& k, Handle handle, int& count);

	size_t addHandle(const std::string& k, Handle handle);

	size_t removeHandle(const std::string& k, Handle handle);

	void clearHandle(const std::string& k);

	size_t clearHandle(Handle handle, KeyRefCountArray& countArray);

	HandleArray getHandles(const std::string& k);

	bool empty();
	size_t size();
	void clear();

	size_t keys(StringArray& ids);

	size_t getCount(KeyRefCountArray& countArray);

public:
	typedef std::set< void* >     HandleSet;

	typedef std::function< bool(HandleSet&) >	HandleSetPredicate;

	bool findDo(const std::string& k, HandleSetPredicate& pred);

protected:
	typedef std::map< std::string, HandleSet>	HandleSetMap;

	typedef HandleSetMap::iterator	iterator;

protected:
	

public:
	static size_t getHandles(HandleSet& handleSet, HandleArray& handles);

protected:

protected:
	std::mutex		m_mutex;
	HandleSetMap	m_map;


};

#endif /* HANDLEREFCOUNTMAP_H_ */
